package com.gs.SynAntiWandering.fragment;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.activity.alert.AlertFilterActivity;
import com.gs.SynAntiWandering.adapter.AlertAdapter;
import com.gs.SynAntiWandering.apiresult.ApiResultData;
import com.gs.SynAntiWandering.apiresult.PagedList;
import com.gs.SynAntiWandering.constant.AllConstants;
import com.gs.SynAntiWandering.constant.EventCode;
import com.gs.SynAntiWandering.control.LinearLayoutManager;
import com.gs.SynAntiWandering.db.bean.Event;
import com.gs.SynAntiWandering.db.dto.AlertDto;
import com.gs.SynAntiWandering.db.query.AlertQuery;
import com.gs.SynAntiWandering.fragment.base.BaseListFragment;
import com.gs.SynAntiWandering.presenter.AlertPresenter;
import com.gs.SynAntiWandering.util.ToastUtil;
import com.gs.SynAntiWandering.view.IAlertView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class AlertFragment extends BaseListFragment<IAlertView, AlertPresenter, AlertDto, AlertQuery> implements IAlertView {

    private AlertAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_alert;
    }

    @Override
    protected AlertPresenter createPresenter() {
        return new AlertPresenter();
    }

    @Override
    protected AlertQuery createQuery() {
        return new AlertQuery();
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        adapter = new AlertAdapter(context, mData, R.layout.rvitem_in_out);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
        mPresenter.getData(query);
    }

    @Override
    protected void search() {
        mData.clear();
        query.init();
        query.filter();
        query.setKeyWord(toolBarSearch.getText());
        mPresenter.getData(query);
    }

    @Override
    public void filter() {
        Intent intent = new Intent(context, AlertFilterActivity.class);
        intent.putExtra(AllConstants.WHAT, query);
        startActivityForResult(intent, 1);
    }

    @Override
    public void onGetDataSuccess(ApiResultData<PagedList<AlertDto>> apiResult) {
        refreshSmartRefreshLayout();
        if (apiResult.getStatus() != 2000) {
            ToastUtil.showToast(apiResult.getMessage());
            adapter.notifyDataSetChanged();
            return;
        }
        mData.addAll(apiResult.getData().getList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onGetDataFails(Exception e) {
        refreshSmartRefreshLayout();
        adapter.notifyDataSetChanged();
        ToastUtil.showToast( R.string.network_error);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event) {
        if (event.getWhat() == EventCode.EVENT_PHOTO_TYPE) {
            search();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            query = (AlertQuery) data.getSerializableExtra(AllConstants.WHAT);
            if (query != null) {
                query.init();
                mData.clear();
                mPresenter.getData(query);
            }
        }
    }
}
